/**
 * COPYRIGHT (C) 2009. Denny Tran, Omar Jaime, David Deng, Julie Hsieh. All Rights Reserved
 * Class is a ViewPanel containing JComponents asking for document information.  
 * @author Denny Tran, Omar Jaime, David Deng, Julie Hsieh 
 * @version 1.01 2009-05-05
 */

package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class AddDocument extends ViewPanel
{
    private JTextField nameField;
    private JTextField fileField;
    private DocumentPanel docPanel;

    /**
     * Construct ViewPanel containing JComponents asking for document information
     * @param viewManager the container of this panel
     * @param identification specific identification used to reference this panel
     */
    public AddDocument(final ViewManager viewManager, String identification)
    {
        super(viewManager, identification);
        
        docPanel = (DocumentPanel) viewManager.getView("document");
        
        JLabel title = new JLabel("Add Document");
        title.setFont(new Font("Serif", Font.PLAIN, 24));
        title.setSize(150, 30);
        title.setLocation(60, 0);
        this.add(title);
        
        Dimension labelSize = new Dimension(50, 20);
        int labelX = 60;
        int labelY = 40;
        int labelSpread = 30;
        
        // labels
        JLabel nameLabel = new JLabel("Name:");
        nameLabel.setSize(labelSize);
        nameLabel.setLocation(labelX,labelY);
        labelY += labelSpread;
        this.add(nameLabel);
    
        JLabel fileLabel = new JLabel("File:");
        fileLabel.setSize(labelSize);
        fileLabel.setLocation(labelX, labelY);
        labelY += labelSpread;
        this.add(fileLabel);
        
        // text fields
        Dimension textFieldSize = new Dimension(150,20);
        int textFieldX = labelX + (int)labelSize.getWidth() + 20;
        int textFieldY = 40;
        // use same spread as labels
        
        nameField = new JTextField();
        nameField.setSize(textFieldSize);
        nameField.setLocation(textFieldX, textFieldY);
        textFieldY += labelSpread;
        this.add(nameField);
        
        fileField = new JTextField();
        fileField.setSize(textFieldSize);
        fileField.setLocation(textFieldX, textFieldY);
        this.add(fileField);

        //buttons
        JButton browseButton = new JButton("Browse");
        browseButton.setSize(80,20);
        browseButton.setLocation(fileField.getX() + fileField.getWidth() + 20, textFieldY);
        browseButton.addActionListener(new ActionListener()
        {
			public void actionPerformed(ActionEvent arg0)
			{
				JFileChooser chooser = new JFileChooser();
				int result = chooser.showDialog(null, "Open");
				if(result == JFileChooser.APPROVE_OPTION)
				{
					fileField.setText(chooser.getSelectedFile().getAbsolutePath());
				}
			}        	
        });
        this.add(browseButton);
        
        JButton confirmButton = new JButton("OK");
        confirmButton.setSize(80,20);
        confirmButton.setLocation(100, 300);
        confirmButton.addActionListener(new ActionListener()
        {
			public void actionPerformed(ActionEvent arg0)
			{
				String name = nameField.getText();
				if(name.isEmpty())
				{
					JOptionPane.showMessageDialog(null, "Insert Document Name");
					return;
				}
				else
				{
					docPanel.addDocument(name, fileField.getText());
					viewManager.showView("document");
				}
			}        	
        });
        this.add(confirmButton);
        
        JButton cancelButton = new JButton("Cancel");
        cancelButton.setSize(80,20);
        cancelButton.setLocation(200, 300);
        cancelButton.addActionListener(viewManager.addLink("document"));
        this.add(cancelButton);
    }

    /**
     * Clears all text fields and check boxes
     */
    public void clearView()
    {
        nameField.setText("");
        fileField.setText("");
    }
}
